package com.example.ui.other;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.huashangapp.R;
import com.example.net.AnalyzingJson;
import com.example.net.NetRequest;
import com.example.ui.contact.face.FaceAdapter;
import com.example.ui.share.SeeActivity;
import com.example.ui.sharte.util.SRecommend;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class FAQActivity extends Activity {
	private Button leftBtn,rightBtn;
	private TextView titleText;
	private ExpandableListView faq_elv;
	private ArrayList<SRecommend> faq_sr;
	private String data;
	private FAQadapter faq_adapter;
	private ImageView faq_ivserch;
	private EditText faq_etserch;
	private String main;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.other_faq);
		new Thread(faq_run).start();
		init();

	}
	Runnable faq_run = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			data = NetRequest.postRequestFAQ();
			faq_sr = AnalyzingJson.getcontent_sra(data);

			h.sendEmptyMessage(1);
		}
	};
	private Handler h = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				
				 faq_adapter.setCcInfos(faq_sr);
				 faq_adapter.notifyDataSetChanged();
				break;
			case 2:
				Toast.makeText(FAQActivity.this, "抱歉，没找到您需要的问题", Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
			}

		};

	};

	private void init() {
		faq_etserch=(EditText) findViewById(R.id.of_etserch);
		faq_ivserch=(ImageView) findViewById(R.id.of_ivserch);
		faq_sr=new ArrayList<SRecommend>();
		faq_adapter=new FAQadapter(this, faq_sr);
		leftBtn = (Button) findViewById(R.id.layout_title_left_btn);
		rightBtn = (Button) findViewById(R.id.layout_title_right_btn);
		titleText = (TextView) findViewById(R.id.layout_title_middle_text);
		leftBtn.setText("返回");
		rightBtn.setVisibility(View.INVISIBLE);
		titleText.setText("常见问题");
		faq_elv=(ExpandableListView) findViewById(R.id.of_elv);	
		faq_elv.setAdapter(faq_adapter);
		leftBtn.setOnClickListener(faq_ocl);
		faq_ivserch.setOnClickListener(faq_ocl);
	}	
	private OnClickListener faq_ocl=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.layout_title_left_btn:
				finish();				
				break;
			case R.id.of_ivserch:
				 main=faq_etserch.getText().toString();
				new Thread(faq_serch).start();
				break;
			default:
				break;
			}
		}
	};
	Runnable faq_serch = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			data = NetRequest.postRequestFAQlike(main);
			JSONObject j;
			try {
				j = new JSONObject(data);
				String result=j.getString("response");
				if(result.equals("true")){
					faq_sr = AnalyzingJson.getcontent_sra(data);			
					h.sendEmptyMessage(1);
				}else{
					h.sendEmptyMessage(2);
					
				}
			} catch (JSONException e) {
				
				e.printStackTrace();
			}

		}
	};

	

	

}
